<template>
  <div>
    <!-- 渲染图表 -->
    <div ref="chartBox" class="chartBox"></div>
  </div>
</template>

<script>
export default {
  name: 'Echart',
  data () {
    return {

      timer: null,
      myChart: null,
      option: {
        tooltip: {},
        radar: {
          // shape: 'circle',
          indicator: [
            { name: '工作效率', max: 100 },
            { name: '考勤', max: 100 },
            { name: '积极性', max: 100 },
            { name: '帮助同事', max: 100 },
            { name: '自主学习', max: 100 },
            { name: '正确率', max: 100 }
          ]
        },
        series: [
          {
            type: 'radar',
            data: [
              {
                value: [10, 1, 100, 5, 100, 0],
                name: '张三'
              },
              {
                value: [50, 50, 50, 50, 50, 10],
                name: '李四'
              },
              {
                value: [5, 5, 5, 0, 50, 10],
                name: '李2'
              }
            ]
          }
        ]
      }
    }
  },
  watch: {
    option: {
      handler: function (newVal) {
        // console.log(newVal)
        this.myChart.setOption(this.option)
      },
      // immediate: true,
      // 如果监听的是个对象，deep可以监听对象属性值的变化
      deep: true
    }
  },
  mounted () {
    // 只能在这里初始化绘制
    this.initChart()
  },
  destroyed () {
    clearInterval(this.timer)
  },
  methods: {
    initChart () {
      // 基于准备好的dom，初始化echarts实例
      this.myChart = this.echarts.init(this.$refs.chartBox)

      this.timer = setInterval(() => {
        this.changeData()
      }, 2000)
      // 绘制图表
      this.myChart.setOption(this.option)
    },
    changeData () {
      const dataChange = []
      for (let i = 0; i < 3; i++) {
        const per = { value: [], name: '阿' + i }
        for (let j = 0; j < 6; j++) {
          const num = Math.floor(Math.random() * 100 + 1)
          per.value.push(num)
        }
        dataChange.push(per)
      }
      this.option.series[0].data = dataChange
      // console.log(dataChange)
    }
  }
}
</script>

<style lang="scss" scoped>
.chartBox {
  width: 100%;
  height: 400px;
}
</style>
